//
//  ZRVisitorView.m
//  SinaByOC
//
//  Created by 张锐 on 16/3/15.
//  Copyright © 2016年 zhangr. All rights reserved.
//

#import "ZRVisitorView.h"
#import <Masonry.h>
@interface ZRVisitorView()
//创建中心图片
@property (nonatomic,strong) UIImageView * centerImageView;
//创建动画图片
@property (nonatomic,strong) UIImageView * animationImageView;
//创建遮罩图片
@property (nonatomic,strong) UIImageView * coverImageView;
//创建文本文字
@property (nonatomic,strong) UILabel * textLabel;
//创建注册按钮
@property (nonatomic,strong) UIButton * registerButton;
//创建登录按钮
@property (nonatomic,strong) UIButton * loginButton;

@end

@implementation ZRVisitorView


/**
 *  对外接口
 *
 *  @param imageName   中间图片名称
 *  @param text        文字
 *  @param isAnimation 是否动画
 */
-(void)setVisitorViewWithImageName:(NSString *)imageName andText:(NSString *)text andAnimation:(BOOL) isAnimation{

    self.centerImageView.image = [UIImage imageNamed:imageName];
    
    self.textLabel.text = text;
    
    if (isAnimation) {
        
        self.animationImageView.hidden = NO;
        [self startAnimation];
    }else{
    
        self.animationImageView.hidden = YES;
    
    }

}

/**
 *  重写构造方法
 */
-(instancetype)init{

    self = [super init];
    
    if (self) {

        [self setupUI];
        
        self.backgroundColor = [[UIColor alloc]initWithWhite:237/255.0 alpha:1];
        
    }
    
    return  self;
}
/**
 *  添加控件设置约束
 */
-(void)setupUI{
  
    [self addSubview:self.animationImageView];
    
    [self addSubview:self.coverImageView];
    
    [self addSubview:self.centerImageView];
    
    [self addSubview:self.textLabel];
    
    [self addSubview:self.registerButton];

    [self addSubview:self.loginButton];
    
//设置所有控件约束
    
    [self.animationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.mas_centerX);
        
        make.centerY.equalTo(self.mas_centerY);
        
    }];
    
    [self.coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.mas_centerX);
        
        make.centerY.equalTo(self.mas_centerY);
        
    }];
    
    [self.centerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.mas_centerX);
        
        make.centerY.equalTo(self.mas_centerY);
        
    }];
    
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.coverImageView.mas_bottom).offset(15);
        
        make.centerX.equalTo(self.mas_centerX);
        
        make.width.equalTo(@216);
        
    }];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.textLabel.mas_bottom).offset(10);
        
        make.left.equalTo(self.textLabel.mas_left);
        
        make.width.equalTo(@100);
        
        make.height.equalTo(@35);
        
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.textLabel.mas_bottom).offset(10);
        
        make.right.equalTo(self.textLabel.mas_right);
        
        make.width.equalTo(@100);
        
        make.height.equalTo(@35);
        
    }];

}
-(void)startAnimation{

    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];

    animation.toValue = @(2 * M_PI);
    
    animation.repeatCount = MAXFLOAT;
    
    animation.duration = 8;
    
    animation.removedOnCompletion = NO;
    
    [self.animationImageView.layer addAnimation:animation forKey:nil];
}

//懒加载所有控件

-(UIImageView *)centerImageView{
    
    if (_centerImageView == nil) {
        
        _centerImageView =[[UIImageView alloc]init];
        
        _centerImageView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
    }
    
    return _centerImageView;
}

-(UIImageView *)animationImageView{
    
    if (_animationImageView == nil) {
        
        _animationImageView = [[UIImageView alloc]init];
        
        _animationImageView.image = [ UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
    }
    
    return _animationImageView;
}

-(UIImageView *)coverImageView{
    
    if (_coverImageView == nil) {
        
        _coverImageView = [[UIImageView alloc]init];
        
        _coverImageView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
    }
    return _coverImageView;
}

-(UILabel *)textLabel{
    
    if (_textLabel == nil) {
        
        _textLabel = [[UILabel alloc]init];
        
        _textLabel.text = @"关注一些人，回这里看看有什么惊喜";
        
        _textLabel.textAlignment = NSTextAlignmentCenter;
        
        _textLabel.textColor = [UIColor darkGrayColor];
        
        _textLabel.numberOfLines = 0;
        
        _textLabel.font = [UIFont systemFontOfSize:14];
    }
    
    return _textLabel;
}

-(UIButton *)registerButton{
    
    if (_registerButton == nil) {
        
        _registerButton =[[UIButton alloc]init];
        
        [_registerButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        
        [_registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        
        _registerButton.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [_registerButton addTarget:self action:@selector(registerButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _registerButton;
    
}

-(UIButton *)loginButton{
  
    if (_loginButton == nil) {
        
        _loginButton = [[UIButton alloc]init];
        
        [_loginButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        
        [_loginButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        
        _loginButton.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [_loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}
/**
 *  注册按钮点击事件
 */
-(void)registerButtonClick{

    //发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ZRVisitorViewRegisterButtonClick" object:nil userInfo:nil];

}
/**
 *  登录按钮点击事件
 */
-(void)loginButtonClick{
    
    //发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ZRVisitorViewLoginButtonClick" object:nil userInfo:nil];

}
@end
